package yxy.game.pm2.active.module.sdk.weixin;

import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.bean.actyaoqingjiangli;
import yxy.game.pm2.active.module.ActOpenHandle;
import yxy.game.pm2.active.module.OpenType;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.bean.sdk.WeiXinOtherParm;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.ArrayList;
import java.util.Map;

/**
 * 成长基金
 * @author Dell
 *
 */
public class ActiveModule_邀请好友 extends PlayerModule {

	//bean -------------------------------
	public static final int acttype = 202;
	
	private static final int minlevel = 5;	//有效邀请等级限制
	public static final String mrinvitecode = "xxxxxxxx"; //默认值 xxxxxxxx  无邀请人或邀请人数据处理完成为默认值
	
	private String Invitecode;	//邀请人code 
	private ArrayList<String> beinviteds = new ArrayList<String>();	//被邀请人codes  满足要求  最大10人
	private ArrayList<Integer> drawids = new ArrayList<Integer>();	//已经领取的id
	private boolean collect;	//是否收藏	
	
	public String getInvitecode() {
		return Invitecode;
	}
	public void setInvitecode(String invitecode) {
		Invitecode = invitecode;
	}
	public ArrayList<String> getBeinviteds() {
		return beinviteds;
	}
	public void setBeinviteds(ArrayList<String> beinviteds) {
		this.beinviteds = beinviteds;
	}
	public ArrayList<Integer> getDrawids() {
		return drawids;
	}
	public void setDrawids(ArrayList<Integer> drawids) {
		this.drawids = drawids;
	}
	public boolean isCollect() {
		return collect;
	}
	public void setCollect(boolean collect) {
		this.collect = collect;
	}

	//func -----------------------------------------------------------------

//	public static final int canget = 1;
//	public static final int hadget = 2;
//	public static final int cannotget = 3;
	
	private static ActiveModule_邀请好友 create(String playerCode, String invitecode) {
		ActiveModule_邀请好友 am = new ActiveModule_邀请好友();
		am.setPlayerCode(playerCode);
		am.setCollect(false);
		am.setInvitecode(invitecode);
		am.set();
		return am;
	}
	
	/**
	 * 处理被邀请者
	 * @param pb
	 */
	public void dealbeinvited(PlayerBean pb) {
		if(getBeinviteds().size() >= 10) {
			return;
		}
		
		if(getBeinviteds().contains(pb.getCode())) {
			return;
		}
		
		if(pb.getLevel() >= minlevel) {
			getBeinviteds().add(pb.getCode());
			set();
		}
	}
	
	/**
	 * 处理被邀请者
	 * @param beinvitedcode
	 */
	public void dealbeinvited(String beinvitedcode) {
		PlayerBean pb = PlayerBean.get(beinvitedcode);
		dealbeinvited(pb);
	}
	
	// redis ---------------------------------------------------
	
	static private String key(String playerCode) {
		return String.format("PLAYER:%s:ACTIVE:%S", playerCode, acttype);
	}

	public void set() {
		try {
			setv(key(getPlayerCode()), this);
			check();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public ActiveModule_邀请好友 get(String playerCode) {
		try {
			PlayerBean pb = PlayerBean.get(playerCode);
			WeiXinOtherParm wxop = WeiXinOtherParm.get(pb.getUid());
			if(wxop == null) {
				return null;
			}
			
			ActiveModule_邀请好友 module = getv(key(playerCode));
			if (module == null) {
				module = create(playerCode, wxop.getInvitecode());	
				try {
					if(WeiXinOtherParm.lockWeiXinOtherParm()) {
						wxop.setInvitecode(WeiXinOtherParm.mrinvitecode);
						WeiXinOtherParm.touch().put(wxop.getUid(), wxop);
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}finally {
					WeiXinOtherParm.unlockWeiXinOtherParm();
				}

			}
			module.check();
			return module;
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return null;
	}

	/** 查看是否有可领取奖励 控制红点 */
	public void check() {
		new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.开启);
		Map<String, actyaoqingjiangli> datamap = ActDataCenter.getAllData(actyaoqingjiangli.class, getPlayerCode());
		for(actyaoqingjiangli data : datamap.values()) {
			if(getDrawids().contains(data.getId())) {
				continue;
			}
			
			if(getBeinviteds().size() >= data.getCondition()) {
				new TipHandle().updateTip(getPlayerCode(), TipType.活动_微信_邀请好友奖励, true);
				return;
			}
		}
		
		new TipHandle().updateTip(getPlayerCode(), TipType.活动_微信_邀请好友奖励, false);
	}
	
	
	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}
}
